package com.putao.spider;

import java.util.LinkedList;
import java.util.List;

import org.jsoup.helper.StringUtil;

import com.putao.entity.FootballData;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class FootballSpider implements PageProcessor {

	private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

	@Override
	public void process(Page page) {
		List<FootballData> list = new LinkedList<>();
		// page.addTargetRequests(page.getHtml().links().regex("(https://dongqiudi\\.com/\\w+/\\w+)").all());
		List<Selectable> tbody = page.getHtml().xpath("//table[@class='list_1']/tbody/").nodes();
		page.addTargetRequests(page.getHtml().xpath("//div[@id=\"stat_list\"]").links()
				.regex("https://www\\.dongqiudi\\.com/data\\?competition=\\d{3}").all());
		page.putField("matchName", page.getHtml().xpath("//div[@id=\"stat_list\"]/a[@class=\"sel\"]/text()"));
		if (page.getResultItems().get("matchName").toString().contains("杯")) {
			return;
		}
		for (int i = 0; i < tbody.size(); i++) {
			FootballData footballData = new FootballData();
			String temp = tbody.get(i).xpath("//tr/td[1]/text()").toString();
			if (!StringUtil.isBlank(temp) && temp.matches("\\d*")) {
				footballData.setRank(new Integer(temp));
				footballData.setTeamName(tbody.get(i).xpath("//tr/td[2]/text()").toString());
				footballData.setPlayCount(new Integer(tbody.get(i).xpath("//tr/td[3]/text()").toString()));
				footballData.setWinCount(new Integer(tbody.get(i).xpath("//tr/td[4]/text()").toString()));
				footballData.setFlatCount(new Integer(tbody.get(i).xpath("//tr/td[5]/text()").toString()));
				footballData.setFailCount(new Integer(tbody.get(i).xpath("//tr/td[6]/text()").toString()));
				footballData.setGoal(new Integer(tbody.get(i).xpath("//tr/td[7]/text()").toString()));
				footballData.setLoseGoal(new Integer(tbody.get(i).xpath("//tr/td[8]/text()").toString()));
				footballData.setGoalDifference(new Integer(tbody.get(i).xpath("//tr/td[9]/text()").toString()));
				footballData.setAccumulated(new Integer(tbody.get(i).xpath("//tr/td[10]/text()").toString()));

				list.add(footballData);
			}
		}
		page.putField("data", list);
	}

	@Override
	public Site getSite() {
		site.setCharset("UTF-8");
		return site;
	}

	public static void main(String[] args) {
		Spider.create(new FootballSpider()).addUrl("https://www.dongqiudi.com/data")
				.addPipeline(new JsonFilePipeline("D:\\webmagic\\")).addPipeline(new ConsolePipeline()).thread(5).run();
	}
}